<--- %%NOBANNER%% --> dtable.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Create a table in the opened word document;                        |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| 4 arguments are needed:                                            |
|   bmark: is the name of bookmark you want to create your table     |
|   ncols: is the number of columns you want to create your table    |
|   width: the width of the entire table;                            |
|   col1width: the width of the 1st column;                          |
|   title: T or F.                                                   |
|          If T, then create two rows, the first row has only one    |
|                 big cell for the title;                            |
|          if F, then create only one row, with number of columens as|
|                user specified.                                     |
|   wordref: word reference; not necessary default is "wordsys";     |
|-------------<-- End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %dtable(bmark='t1', ncols=4, width=6.5);                  |
| Usage:   %dtable(bmark=,ncols=2,width=6,col1width=.,title=F,       |
|                  wordref=wordsys);                                 |
\-------------------<-- End of Files Created-->---------------------*/
%macro dtable(bmark=,ncols=2,width=6,col1width=.,title=F,wordref=wordsys) ;
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  2-27-2001 11:14pm;                |
| Modified: 12-25-2001 2:11pm;                |
| Purpose:  Create an empty table in the word |
|           document;                         |
\--------------------------------------------*/
%let bookmark=%qscan(&bmark,1,%str(,()''""));
%let clwth=%sysevalf(&width/&ncols);
%if (&col1width eq) or (&col1width = .) %then %do;
   %do _i_=1 %to &ncols;
      %let var&_i_=%sysevalf(&width/&ncols);
   %end;
%end;
%else %do;
   %let hwidth=%sysevalf(&width-&col1width);
   %let var1=&col1width;
   %do _i_=2 %to &ncols;
      %let var&_i_=%sysevalf(&hwidth/%sysevalf(&ncols-1));
   %end;
%end;

%if (%quote(%upcase(&title))=F) %then %do;
data _null_;
     file &wordref lrecl=2000;
     length str $2000.;
     /*The table has 4 columns, 2 rows. Here I use format 16: single border table;
    .Format="28", .Format="31", .Format = "18", is all the options */;
     put '[ViewNormal]';
     put '[InsertPara]';
     str='[TableInsertTable .ConvertFrom = "", .NumColumns = "'||trim(left(put(&ncols, 3.0)))||'",
        .NumRows = "1", .InitialColWidth = "'||trim(left(put(&clwth, 4.2)))||'" + Chr$(34),
        .Format = "16", .Apply = "167"]';
     put str;
     /*Set the first column to 2.8,  space between columns to be 0.05*/
     str='[TableColumnWidth .ColumnWidth = "'||trim(left(put(&var1, 4.2)))||' " + Chr$(34),
       .SpaceBetweenCols = "0.05" + Chr$(34), .RulerStyle = "0"]';
     put str;

     /*Move the cursor right one cell*/
     %do _i_=2 %to &ncols;
        put '[CharRight 1]';

      /*Push 'shift + right arrow' select the second and the third column*/
      /*Set the first column to 1.18,  space between columns to be 0.05*/
         str='[TableColumnWidth .ColumnWidth = "'||trim(left(put(&&var&_i_, 4.2)))||'" + Chr$(34),
           .SpaceBetweenCols = "0.05" + Chr$(34), .RulerStyle = "0"]';
      put str;
     %end;
     put '[StartOfRow]';
     put '[StartOfColumn]';
        %if &bookmark ne %then %do;
         put '[Tableselecttable]';
             /*Create a bookmark*/
             str='[EditBookmark .Name = "'||trim(left("&bookmark"))||'", .SortBy = 0, .Add]';
           put str;
       %end;
   run;
%end;
%else %do;
data _null_;
  file &wordref lrecl=2000;
  length str $2000.;
  /*The table has 4 columns, 2 rows. Here I use format 16: single border table;
    .Format="28", .Format="31", .Format = "18", is all the options */;
  put '[ViewNormal]';
  put '[InsertPara]';
     str='[TableInsertTable .ConvertFrom = "", .NumColumns = "'||trim(left(put(&ncols, 3.0)))||'",
        .NumRows = "2", .InitialColWidth = "'||trim(left(put(&clwth, 4.2)))||'" + Chr$(34), .Format = "16", .Apply = "167"]';
  put str;

  /*Push 'shift + down arrow' select the first column*/
  put '[LineDown 1, 1]';

  /*Set the first column to 2.8,  space between columns to be 0.05*/
     str='[TableColumnWidth .ColumnWidth = "'||trim(left(put(&var1, 4.2)))||' " + Chr$(34),
       .SpaceBetweenCols = "0.05" + Chr$(34), .RulerStyle = "0"]';
  put str;

  /*Move the cursor right one cell*/
  %do _i_=2 %to &ncols;
        put '[CharRight 1]';

        /*Push 'shift + down arrow' select the first column*/
        put '[LineDown 1, 1]';
        %*put '[FormatFont .Bold=0]'; /*Set font format to bold*/

        /*Push 'shift + right arrow' select the second and the third column*/
    /*Set the first column to 1.18,  space between columns to be 0.05*/
           str='[TableColumnWidth .ColumnWidth = "'||trim(left(put(&&var&_i_, 4.2)))||'" + Chr$(34),
          .SpaceBetweenCols = "0.05" + Chr$(34), .RulerStyle = "0"]';
        put str;
        %end;

        /*Push 'shift + down arrow' select the last column*/
        /*Move the cursor up one cell*/
        put '[NextCell]';

        /*Push 'shift + left arrow' three times: select the firt row*/
    %let numcells=%eval(&ncols-1);
        %do _j_=1 %to &numcells;
                put '[CharLeft 1, 1]';
                %*put '[FormatFont .Bold=0]'; /*set font format to bold*/
                %end;
    put '[TableMergeCells]'; /*Merge cells*/
    %if &bookmark ne %then %do;
          /*Create a bookmark*/
          str='[EditBookmark .Name = "'||trim(left("&bookmark"))||'", .SortBy = 0, .Add]';
        put str;
    %end;
    run;
%end;
%mend dtable;